package com.koron.car.web.applycar.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.car.web.applycar.bean.entity.DriverSchedule;
import com.koron.car.web.applycar.bean.query.LastScheduleQuery;
import com.koron.car.web.applycar.service.DriverScheduleService;
import com.koron.common.core.config.ThreadLocalContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author twcao
 * @title: DriverScheduleController
 * @projectName GDH_EAM_V2
 * @description: 用车调度
 * @date 2022/3/918:00
 */
@Slf4j
@RestController
@RequestMapping("/driver/schedule")
@Api(tags = "车辆管理-用车调度")
public class DriverScheduleController {

    @Autowired
    private DriverScheduleService scheduleService;

    @GetMapping("/selectOne")
    @ApiOperation("根据申请id查询调度详情")
    public Response<DriverSchedule> selectOne(String applyId) {
        log.info("根据申请id查询调度详情， applyId = {}", applyId);
        DriverSchedule bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), scheduleService, "getByApplyId", DriverSchedule.class, applyId);
        return Response.ok(bean);
    }

    @GetMapping("/getLastSchedule")
    @ApiOperation("根据司机或者车辆，查询上次调度信息(carId或driverId，至少一个不为空, 同时不为空时，以车辆id为主)")
    public Response<DriverSchedule> selectOne(LastScheduleQuery query) {
        log.info("根据司机或者车辆，查询上次调度信息， query = {}", JSON.toJSONString(query));
        DriverSchedule bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), scheduleService, "getLastSchedule", DriverSchedule.class, query);
        return Response.ok(bean);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("创建/编辑用车调度信息")
    public Response<DriverSchedule> saveOrUpdate(@RequestBody DriverSchedule schedule) {
        log.info("创建/编辑用车调度信息， schedule = {}", JSON.toJSONString(schedule));
        DriverSchedule bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), scheduleService, StringUtils.isBlank(schedule.getId()) ? "insert" : "update", DriverSchedule.class, schedule);
        return Response.ok(bean);
    }

    @GetMapping("/checkScheduleTime")
    @ApiOperation("检查调度时间是否冲突")
    public Response<Map> checkScheduleTime(DriverSchedule schedule){
        log.info("检查调度时间，schedule = {}",schedule);
        return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), scheduleService, "checkScheduleTime", Response.class, schedule);
    }
}
